//
//  SildeLineSprite.m
//  StarBugs
//
//  Created by ?? ? on 12/29/11.
//  Copyright 2011 antisword@playorca.com. All rights reserved.
//

#include "Object/SideLineSprite.h"
#include "Util/ImageManager.h"

SideLineSprite::~SideLineSprite()
{

}

SideLineSprite::SideLineSprite()
{
    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "sideLine");
    addChild(mainSprite);
    objectType = objectType_sideLine;
    
    reflectArray = CCArray::create();
    reflectArray->retain();

    
    reFlectFrame[0] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "sideLine2");
    reFlectFrame[0]->retain();
    reFlectFrame[1] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "sideLine3");
    reFlectFrame[1]->retain();
    reFlectFrame[2] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "sideLine");
    reFlectFrame[2]->retain();
}

void SideLineSprite::setBody(b2World* world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    bodyDef.position.Set(position.x / PTM_RATIO, position.y / PTM_RATIO);
    bodyDef.userData = mainSprite;
    
    b2FixtureDef fixtureDef;
	mainBody = world->CreateBody(&bodyDef);
	b2PolygonShape dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 1.0f;

    dynamicBox.SetAsBox(2.f / PTM_RATIO, 28.f / PTM_RATIO);
    fixtureDef.shape = &dynamicBox;
    mainBody->CreateFixture(&fixtureDef);
    
    mainBody->SetTransform(mainBody->GetPosition(), bodyAngle * b2_pi / 180);
}

void SideLineSprite::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void SideLineSprite::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void SideLineSprite::startReflectAction()
{
    cnt = 0;
    unscheduleAllSelectors();
    schedule(schedule_selector(SideLineSprite::reflectTick), 0.05);
}

void SideLineSprite::reflectTick(float dt)
{
//    if(cnt > 5)
//    {
//        mainSprite->setDisplayFrame((CCSpriteFrame *)reflectArray->objectAtIndex(2));
//        unschedule(schedule_selector(SideLineSprite::reflectTick));
//        return;
//    }
//    
//    int tmp = cnt % 2;
//    mainSprite->setDisplayFrame((CCSpriteFrame *)reflectArray->objectAtIndex(tmp));
//    cnt ++;
    
    if(cnt > 5)
    {
        mainSprite->setDisplayFrame(reFlectFrame[2]);
        unschedule(schedule_selector(SideLineSprite::reflectTick));
        return;
    }
    
    int tmp = cnt % 2;
    mainSprite->setDisplayFrame(reFlectFrame[tmp]);
    cnt ++;
}